package com.funtl.screw.common.core.util;

import com.funtl.screw.common.core.constant.CommonConstants;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @param <T>
 * @author Lusifer
 */
@Setter
@Getter
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@ApiModel(value = "响应信息主体")
public class ResponseResult<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "返回标记：成功标记=0，失败标记=1")
    private int code;

    @ApiModelProperty(value = "返回信息")
    private String msg;

    @ApiModelProperty(value = "数据")
    private T data;

    private ResponseResult<T> code(int code) {
        this.code = code;
        return this;
    }

    private ResponseResult<T> msg(String msg) {
        this.msg = msg;
        return this;
    }

    private ResponseResult<T> data(T data) {
        this.data = data;
        return this;
    }

    public static <T> ResponseResult<T> ok() {
        return restResult(null, CommonConstants.SUCCESS, null);
    }

    public static <T> ResponseResult<T> ok(String msg) {
        return restResult(null, CommonConstants.SUCCESS, msg);
    }

    public static <T> ResponseResult<T> ok(T data) {
        return restResult(data, CommonConstants.SUCCESS, null);
    }

    public static <T> ResponseResult<T> ok(T data, String msg) {
        return restResult(data, CommonConstants.SUCCESS, msg);
    }

    public static <T> ResponseResult<T> failed() {
        return restResult(null, CommonConstants.FAIL, null);
    }

    public static <T> ResponseResult<T> failed(String msg) {
        return restResult(null, CommonConstants.FAIL, msg);
    }

    public static <T> ResponseResult<T> failed(T data) {
        return restResult(data, CommonConstants.FAIL, null);
    }

    public static <T> ResponseResult<T> failed(T data, String msg) {
        return restResult(data, CommonConstants.FAIL, msg);
    }

    private static <T> ResponseResult<T> restResult(T data, int code, String msg) {
        return new ResponseResult<T>().code(code).data(data).msg(msg);
    }

}
